package com.hcss.common.redis.base.impl;

import com.hcss.common.redis.base.RedisListDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @描述: redis list类型数据操作Dao接口实现
 * @作者: xuqian
 * @创建日期: 2018-01-12 18:09
 * @公司 浙江鸿程计算机系统有限公司
 */
@Repository("redisListDao")
public class RedisListDaoImpl<T> extends BaseRedisDaoImpl<T> implements RedisListDao<T> {
    private final ListOperations<String, T> listOperations;

    @Autowired
    public RedisListDaoImpl(RedisTemplate redisTemplate) {
        super(redisTemplate);
        this.listOperations = redisTemplate.opsForList();
    }

    @Override
    public ListOperations getListOperations() {
        return listOperations;
    }

    /**
     * 默认插入队列尾部
     * @param key
     * @param value
     */
    @Override
    public void insert(String key, T value) {
        listOperations.rightPush(key, value);
    }

    /**
     * 默认从队列首部开始取一个数据
     * @param key
     * @return
     */
    @Override
    public T selectObject(String key) {
        return get(key, 0);
    }

    @Override
    public void rightInsert(String key, T value) {
        insert(key, value);
    }

    @Override
    public void rightInsertAll(String key, List<T> list) {
        listOperations.rightPushAll(key, list);
    }

    @Override
    public void rightInsertAll(String key, T[] values) {
        listOperations.rightPushAll(key, values);
    }

    @Override
    public void leftInsert(String key, T value) {
        listOperations.leftPush(key, value);
    }

    @Override
    public void leftInsertAll(String key, List<T> list) {
        listOperations.leftPushAll(key, list);
    }

    @Override
    public void leftInsertAll(String key, T[] values) {
        listOperations.leftPushAll(key, values);
    }

    @Override
    public List<T> selectAll(String key) {
        return selectList(key, 0 ,-1);
    }

    @Override
    public List<T> selectList(String key, long start, long end) {
        return listOperations.range(key, start, end);
    }

    @Override
    public void insertLeft(String key, T pvalue, T value) {
        listOperations.leftPush(key, pvalue, value);
    }

    @Override
    public void insertRight(String key, T pvalue, T value) {
        listOperations.rightPush(key, pvalue, value);
    }

    @Override
    public void subList(String key, long start, long end) {
        listOperations.trim(key, start, end);
    }

    @Override
    public Long getSize(String key) {
        return listOperations.size(key);
    }

    @Override
    public void setValue(String key, T value, long index) {
        listOperations.set(key, index, value);
    }

    @Override
    public T get(String key, long index) {
        return listOperations.index(key, index);
    }

    @Override
    public void remove(String key, T value) {
        listOperations.remove(key, 1, value);
    }


}
